/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import Merlion.MRP.bean.MaterialManagementBeanRemote;
import Merlion.SCM.bean.InventoryItemsBeanRemote;
import Merlion.util.state.BOMEntryState;
import Merlion.util.state.BillOfMaterialState;
import Merlion.util.state.CreditProfileState;
import Merlion.util.state.InventoryState;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author cute_jes91
 */
@ManagedBean(name = "BillOfMaterialManagedBean")
@ViewScoped
public class BillOfMaterialManagedBean {

    private Long productId;
    private String productName;
    private List<InventoryState> products;
    private BOMEntryState[] bomEntries;
    private BillOfMaterialState bomState;
    private List<BillOfMaterialState> bomStates;
    private int batchSize;
    private String batchUnit = "No Description Found";
    private List<BOMEntryState> entries;
    private BOMEntryState entry;
    private Long materialId;
    private double quantity;
    private String unit;
    private List<InventoryState> materials;
    @EJB
    private InventoryItemsBeanRemote iib;
    @EJB
    private MaterialManagementBeanRemote mmb;

    /** Creates a new instance of BillOfMaterialManagedBean */
    public BillOfMaterialManagedBean() {
    }

    public void initView(PhaseEvent event) {
        System.out.println("**BillOfMaterialManagedBean : initView()");
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);

        System.out.println("**BillOfMaterialManagedBean : get all products");
        products = iib.viewProducts();
        materials = iib.viewMaterials();

    }

    public void doHandleUpdateBillOfMaterials() {
        if (productId != null) {
            try {
                bomState = mmb.viewProductBillOfMaterial(productId);
                this.setBomState(bomState);
                this.setProductId(bomState.getProductId());
                this.setProductName(bomState.getProductName());
                this.setBatchSize(bomState.getBatchSize());
                if (bomState.getBatchUnit() != null) {
                    this.setBatchUnit(bomState.getBatchUnit());
                }
                this.setEntries(bomState.getEntries());
                for (Object o : bomState.getEntries()) {

                    BOMEntryState bomes = (BOMEntryState) o;
                    this.setMaterialId(bomes.getMaterialId());
                    this.setQuantity(bomes.getQuantity());
                    this.setUnit(bomes.getUnit());

                }
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }

    }
    
    public void doAddNewRecord(ActionEvent event){
        System.out.println("**BillOfMaterialManagedBean: doAddNewRecord() for material id: " + materialId);
        mmb.createBOMEntry(productId, materialId, quantity, unit);
        
        System.out.println("**BillOfMaterialManagedBean: A material record is successfully added");
        
        try{
            FacesContext.getCurrentInstance().getExternalContext().redirect("BillOfMaterials.xhtml");
        }catch(Exception ex){
            ex.printStackTrace();
        }
        
    }
    
    public void doDelete(ActionEvent event) {
        System.out.println("**BillOfMaterialManagedBean: doDelete()");

        try {
            if (entry != null) {
                mmb.deleteBOMEntry(entry.getId());
                System.out.println("**BillOfMaterialManagedBean: Deletion of a Bill Of Material is successful.");
            } else {
                System.out.println("**BillOfMaterialManagedBean: Bill Of Material data is not found.");
            }
            
            FacesContext.getCurrentInstance().getExternalContext().redirect("BillOfMaterials.xhtml");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void onEditRow(RowEditEvent event) {
        System.out.println("**BillOfMaterialManagedBean: onEditRow()");
       
        try {
            BOMEntryState bomes = (BOMEntryState) event.getObject();

            if (bomes != null) {
                Long eEntryId = bomes.getId();
                Long eMaterialId = bomes.getMaterialId();
                double eBatchSize = bomes.getQuantity();
                String eBatchUnit = bomes.getUnit();

                mmb.updateBOMEntry(eEntryId, eMaterialId, eBatchSize, eBatchUnit);
                System.out.println("**BillOfMaterialManagedBean :  A BOM entry is successfully edited.");
            } else {
                System.out.println("**BillOfMaterialManagedBean: No BOM entry data found. onEditRow() failed.");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void doUpdateBatchSize(ActionEvent event) {
        System.out.println("**BillOfMaterialMangedBean : doUpdateBatchSize()");
        System.out.println("BATCH SIZE: " + batchSize);
        mmb.updateBillOfMaterial(productId, batchSize, batchUnit);
        System.out.println("**BillOfMaterialMangedBean : Batch Size is updated");
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Successful", "Changes have been saved"));


    }

    public Long getProductId() {
        return productId;
    }

    public void setProductId(Long productId) {
        this.productId = productId;
    }

    public List<InventoryState> getProducts() {
        return products;
    }

    public void setProducts(List<InventoryState> products) {
        this.setProducts(products);
    }

    public InventoryItemsBeanRemote getIib() {
        return iib;
    }

    public void setIib(InventoryItemsBeanRemote iib) {
        this.iib = iib;
    }

    public BOMEntryState[] getBomEntries() {
        return bomEntries;
    }

    public void setBomEntries(BOMEntryState[] bomEntries) {
        this.setBomEntries(bomEntries);
    }

    public MaterialManagementBeanRemote getMmb() {
        return mmb;
    }

    public void setMmb(MaterialManagementBeanRemote mmb) {
        this.mmb = mmb;
    }

    public BillOfMaterialState getBomState() {
        return bomState;
    }

    public void setBomState(BillOfMaterialState bomState) {
        this.bomState = bomState;
    }

    public List<BillOfMaterialState> getBomStates() {
        return bomStates;
    }

    public void setBomStates(List<BillOfMaterialState> bomStates) {
        this.bomStates = bomStates;
    }

    public int getBatchSize() {
        return batchSize;
    }

    public void setBatchSize(int batchSize) {
        this.batchSize = batchSize;
    }

    public String getBatchUnit() {
        return batchUnit;
    }

    public void setBatchUnit(String batchUnit) {
        this.batchUnit = batchUnit;
    }

    public List<BOMEntryState> getEntries() {
        return entries;
    }

    public void setEntries(List<BOMEntryState> entries) {
        this.entries = entries;
    }

    public BOMEntryState getEntry() {
        return entry;
    }

    public void setEntry(BOMEntryState entry) {
        this.entry = entry;
    }

    public Long getMaterialId() {
        return materialId;
    }

    public void setMaterialId(Long materialId) {
        this.materialId = materialId;
    }

    public double getQuantity() {
        return quantity;
    }

    public void setQuantity(double quantity) {
        this.quantity = quantity;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public List<InventoryState> getMaterials() {
        return materials;
    }

    public void setMaterials(List<InventoryState> materials) {
        this.materials = materials;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }
}
